
const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {


    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    @property(cc.Node)
    role:cc.Node = null;
    
    private x:number = 0;
    private y:number = 0;

    private winp:cc.Size;
    start () {
        enum KEYCODE {
            LEFT = 37,
            UP,
            RIGHT,
            BOTTON
        }
        this.node.x = 6000;
        this.node.y = -480;

        this.getComponent(cc.Camera).zoomRatio = 0.4;


        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,(e:cc.Event.EventKeyboard)=> {

            let key:number = e.keyCode;
            if(KEYCODE.LEFT === key){
                this.x = -10;
            }
            if(KEYCODE.RIGHT === key){
                this.x = 10;
            }
            if(KEYCODE.UP === key){
                this.y = 10;
            }
            if(KEYCODE.BOTTON === key){
                this.y = -10;
            }
        })
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,(e)=> {
            let key:number = e.keyCode;
            this.x = 0;
            this.y = 0;

            // if(KEYCODE.LEFT === key){
            //     this.x = 0;
            // }
            // if(KEYCODE.RIGHT === key){
            //     this.x = 0;
            // }
            // if(KEYCODE.UP === 0){
            //     this.y = 0;
            // }
            // if(KEYCODE.BOTTON === 0){
            //     this.y = 0;
            // }
        });

        window["role"] = this;
        let zoom = 0.025;
        let winp:cc.Size = cc.size(cc.winSize.width / zoom, cc.winSize.height / zoom);
        this.winp = winp;
    }

    update (dt) {
        this.node.x += this.x;
        this.node.y += this.y;

        this.upRole(cc.v2(this.node.x,this.node.y))
    }

    upRole(v2:cc.Vec2){
        this.role.x = (v2.x - 480) / 9600 * this.role.parent.width;
        this.role.y = -(v2.y - 320)/ 9600 * this.role.parent.height;
      // console.log(this.role.position)
    }
}
